#include "terraingrid.h"
#include "global.h"

//--------------------------------------------------------------------------------------
// 

TerrainGrid::TerrainGrid()
{
}

TerrainGrid::~TerrainGrid()
{
	if( m_pVB != NULL )
		m_pVB->Release();
}

bool TerrainGrid::SetSize(int iSize)
{
	// 65*65
    //TERRAINGRIDVERTEX vertices[] =
    //{
    //    { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
    //    { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
    //    {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
    //};
	
	TERRAINGRIDVERTEX vertices[65*65];
	for (int z = 0; z < 65; ++z)
	{
		for (int x = 0; x < 65; ++x)
		{
			int iIndex = z * iSize + x;
			vertices[iIndex].x = x;
			vertices[iIndex].y = 2;
			vertices[iIndex].z = z;
			vertices[iIndex].rhw = 1.0f;
			vertices[iIndex].color = 0x0000ff00;
		}
	}

    // Create the vertex buffer. Here we are allocating enough memory
    // (from the default pool) to hold all our 3 custom vertices. We also
    // specify the FVF, so the vertex buffer knows what data it contains.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 65*65*sizeof(TERRAINGRIDVERTEX),
                                                  0, D3DFVF_TERRAINGRIDVERTEX,
                                                  D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
    {
        return false;
    }

    // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
    // gain access to the vertices. This mechanism is required becuase vertex
    // buffers may be in device memory.
    VOID* pVertices;
    if( FAILED( m_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
        return false;
    memcpy( pVertices, vertices, sizeof(vertices) );
    m_pVB->Unlock();

    return true;
}

bool TerrainGrid::Render()
{
	if (g_pd3dDevice == 0)	return false;

	   // Clear the backbuffer to a blue color
    //g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

	if ( SUCCEEDED(g_pd3dDevice->SetFVF( D3DFVF_TERRAINGRIDVERTEX ) ) )
		if (SUCCEEDED( g_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(TERRAINGRIDVERTEX) ) ) )	
		{
			// Begin the scene
			if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
			{
				// Draw the triangles in the vertex buffer. This is broken into a few
				// steps. We are passing the vertices down a "stream", so first we need
				// to specify the source of that stream, which is our vertex buffer. Then
				// we need to let D3D know what vertex shader to use. Full, custom vertex
				// shaders are an advanced topic, but in most cases the vertex shader is
				// just the FVF, so that D3D knows what type of vertices we are dealing
				// with. Finally, we call DrawPrimitive() which does the actual rendering
				// of our geometry (in this case, just one triangle).
				if (SUCCEEDED( g_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(TERRAINGRIDVERTEX) ) ) )
					if ( SUCCEEDED(g_pd3dDevice->SetFVF( D3DFVF_TERRAINGRIDVERTEX ) ) )
					{
						HRESULT result = g_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 65*65/2 );
						if (result == S_FALSE)
						{
							int i = 0;
						}
					}
					// End the scene
					g_pd3dDevice->EndScene();
			}
			else
				return false;
		}

    // Present the backbuffer contents to the display
    //g_pd3dDevice->Present( NULL, NULL, NULL, NULL );

	return true;
}
